var objectState = {
  // COM飞机状态
    AI_INVALID  : 0,
    AI_DEATH    : 1,
    AI_DANGER   : 2,
    AI_NORMAL   : 3,
    AI_BIRTH    : 4,
};

var objectConst = {
	 // 获得可变对象中某一属性的值时用
	GOV_PICKUP_TYPE : 1,
	GOV_PICKUP_COUNT : 2,
	GOV_DAMAGE : 3,
	GOV_CAN_TRACE : 4,	
};

var objectFlag = {
    TFLAG_HAVE_SHADOW   : (1 << 0),		// 是否有影子
    TFLAG_START_PARENT  : (1 << 1),		// 初始以父物体为基准
    TFLAG_STICK_PARENT  : (1 << 2),		// 跟随父物体
    TFLAG_DIE_PARENT    : (1 << 3),		// 跟随父物体一起爆
    TFLAG_WINK_RED      : (1 << 4),		// 血少了是否闪红
    TFLAG_WINK_WHITE    : (1 << 5)		// 挨打了是否闪白
}

class object {
  constructor(modal, objData) {
    // 是否有效，设置为无效将被程序删除，碰撞不能采用这个
    this.state = objectState.AI_BIRTH;
    // 坐标,对象中心点（碰撞和画都用这个）

    this.x = objData.x;
    this.y = objData.y;
    // 速度量
    this.x_vel = objData.vx;
    this.y_vel = objData.vy;

    //模板数据
    this.modal = modal;

    //显示图片索引
    this.image_index = 0;
    //显示延时
    this.image_interval = 0;

    this.current_life = Const.FORCE_ENIMY_LIFE > 0 ? Const.FORCE_ENIMY_LIFE : modal.vdata.life;
    
    this.spriteNumber = 1;
    this.sprites = [];
  }
  
  setparent(parent) {
    this.parent = parent;
  }
  
  init() {
	for(let i = 0;i < this.spriteNumber;i++) {
		let sprite = SpriteUtil.sprite(this.modal.frames, -1000, 0);	// 新建的时候不要画
		if(!sprite)
			debugger;
//		sprite.x = this.x - sprite.width / 2;
//		sprite.y = this.y - sprite.height / 2;
		app.stage.children[this.modal.layer].addChild(sprite);
		this.sprites.push(sprite);
	}
  }
  
  destroy() {
	for(let i of this.sprites)
	  app.stage.children[this.modal.layer].removeChild(i);
  }

  /*
  *功能：对象的自更新处理
  */
  update(tick) {
    this.x += this.x_vel;
    this.y += this.y_vel;

    if(this.state == objectState.AI_DEATH) {
      this.state = objectState.AI_INVALID;
    } else if(this.x + this.modal.width / 2 < 0 || this.x - this.modal.width / 2 > Const.SCR_WIDTH ||
      this.y + this.modal.height / 2 < 0 || this.y - this.modal.height / 2 > Const.SCR_HIGH) { //出界默认清理掉
      this.state = objectState.AI_DEATH;
    } else {
      if(this.modal.image_count_per_dir > 1) {
        this.image_interval ++;
        if(this.image_interval >= this.modal.image_interval) {
          this.image_interval = 0;
          this.image_index ++;
          if(this.image_index >= this.modal.image_count_per_dir) {
            this.image_index = 0;
          }
        }
      }
    }

    if(this.state == objectState.AI_BIRTH) {
      this.state = objectState.AI_NORMAL;
    }
  }

  _dir(x_vel, y_vel) {
    let dir = 0;
    switch (this.modal.dir_count) {
      case 2:
        return x_vel > 0.1 ? 1 : 0;
      case 4:
        if(x_vel > 0.1) return 1;
        if(y_vel < -0.1) return 2;
        if(y_vel > 0.1) return 3;
        return 0;
      case 8:
        dir = calcDir.get8(x_vel, y_vel);
        return dir >= 8 ? 0 : dir;
      case 16:
        dir = calcDir.get16(x_vel, y_vel);
        return dir >= 16 ? 0 : dir;
      case 32:
        dir = calcDir.get32(x_vel, y_vel);
        return dir >= 32 ? 0 : dir;
      case 64:
        dir = calcDir.get64(x_vel, y_vel);
        return dir >= 64 ? 0 : dir;
      default:
        return 0;
    }
  }
  
  oncollide(shoot) {
  	let dam = shoot.getobjectvar(objectConst.GOV_DAMAGE);
  	this.current_life -= dam;
  	if(this.current_life <= 0 && this.state > objectState.AI_DEATH) {
  		this.state = objectState.AI_DEATH;
  		if(this.modal.vdata.blastseqid > 0) {
  	  		let blast = game.stageSeq[this.modal.vdata.blastseqid];
  			for(let i = 0; i < blast.objs.length; i++)
  				game.new_object_seq(blast, i, this);
  		}
  	}
  }
  
  getobjectvar(nID) {
  	switch(nID)
  	{
  	case objectConst.GOV_DAMAGE:
  		//m_ptemplate不可能为空
  		return this.modal.vdata.damage;
  		break;
  	default:
  		return 0;
  	}
  }
  
  //操作PIXI有关的东西统一放在这个方法，如果换引擎方便点
  render() {
//    if(!this.modal.framesInfo[this.image_index] || !this.modal.framesInfo[this.image_index].centerX && this.modal.framesInfo[this.image_index].centerX != 0) {
//    	debugger;//TODO: 不明原因this.image_index值没有设置
//    	
//    }
      
    this.drawimage(0, this.image_index,  - this.modal.framesInfo[this.image_index].centerX,
      - this.modal.framesInfo[this.image_index].centerY);
  }
  
  render_shadow() {
	  
  }
  
  drawimage(spriteIndex, index, offsetx = 0, offsety = 0) {
	this.sprites[spriteIndex].x = this.x + offsetx;
	this.sprites[spriteIndex].y = this.y + offsety;
	this.sprites[spriteIndex].show(index);
	if(this.usedSprite)
		this.usedSprite[spriteIndex] = true;
	this.sprites[spriteIndex].visible = true;
  }
  
  hide_unused_image() {
	  for(let i = 0;i < this.usedSprite.length;i++)
		  if(!this.usedSprite[i])
			  this.hideimage(i);
  }
  
  hideimage(spriteIndex) {
	  this.sprites[spriteIndex].visible = false;
  }
  
  drawascii(index, offsetx, offsety, subwidth, subheight, subgrid, asc, margin) {
	  
  }
  
  drawnumber(spriteIndex, index, offsetx, offsety, number, margin, digit = 1) {
	if(number < 0)
	  return;
	if(digit == 1)
		this.drawimage(spriteIndex, index + number, offsetx, offsety);
	else {
		let str = number.toString();
		offsetx += (digit - str.length) * margin;
		for(let i = 0;i < str.length;i++)
			this.drawimage(spriteIndex + i, index + parseInt(str[i]), offsetx + margin * i, offsety);
	}
  }

  playsound(index, loop) {
   if(index < this.modal.sound.length) {
	 if(!this.modal.sound[index])
		 return;
     if(loop) {
       this.modal.sound[index].loop = true;
     }
     this.modal.sound[index].play();
   }
  }
}